/**
 * 页面初始化
 */
$(function () {
    //异步请求新闻类型
    $.ajax({
        type: 'get',
        url: '/newsType',
        data: {},
        success: function (data) {
            if (data.message == "success") {
                $(data.list).each(function (index,item) {
                    let newsType = "<div>\n" +
                        "                    <a href=\"#\" onclick=\"findById("+item.id+",'/html/news_and_trends.html')\">\n" +
                        "                        <span>"+item.name+"</span>\n" +
                        "                    </a>\n" +
                        "                </div>";

                    $("#newsType").append(newsType);
                })
                //异步请求分页新闻
                pageNews(1,10);
                return false;
            }
            layer.msg('查询失败，请稍后重试！', {icon: 2, time: 1200});
            return false;
        },
        error: function () {
            layui.layer.msg('请求接口异常,请刷新重试！',{icon:0,time:1300});
            return false;
        }
    })

})

/**
 * 分页
 */
function pageNews(page,limit) {

    findAllNews(page,limit);

}

/**
 * 分页请求新闻
 * @param page
 * @param limit
 */
function findAllNews(page,limit) {
    $("#newsList").html("");
    $("#news-pages").html("");
    var id = sessionStorage.getItem("id");
    // console.log("typeId="+id);
    var data = {'page':page,'limit':limit};
    if (id != null) {
        data = {'page':page,'limit':limit,'typeId':id};
    }
    $.ajax({
        type: 'get',
        url: '/newsTrends',
        data: data,
        success: function (data) {
            let news = "<div class=\"cc\"></div>\n" +
                "                <div class=\"cc\"></div>\n" +
                "                <div class=\"cc\"></div>";
            $("#newsList").append(news);
            if (data.message == "success") {
                // 渲染新闻
                $(data.list).each(function (index,item) {
                    let news = "<div class=\"news-title\">\n" +
                        "                    <div class=\"type-nav\">\n" +
                        "                    <a href=\"#\" onclick=\"findById("+item.typeId+",'/html/news_and_trends.html')\">\n" +
                        "                            <span>["+item.newsType+"]</span>\n" +
                        "                        </a>\n" +
                        "                    </div>\n" +
                        "                    <div class=\"type-2nav\">\n" +
                        "                    <a href=\"#\" onclick=\"findById("+item.id+",'/html/news_details.html')\">\n" +
                        "                            <span title=\""+item.title+"\" class=\"spana\">"+item.title+"</span>\n" +
                        "                        </a>\n" +
                        "                    </div>\n" +
                        "                    <div class=\"type-nav\">\n" +
                        "                        <a href=\"javascript:;\" class=\"a-none\">\n" +
                        "                            <span>"+item.releaseDate+"</span>\n" +
                        "                        </a>\n" +
                        "                    </div>\n" +
                        "                </div>";

                    $("#newsList").append(news);
                })
                // 分页
                //上一页
                let pageNum = data.pageNum>0?data.pageNum-1:0;
                // console.log("上一页="+pageNum);
                let newsPages = "<div>\n" +
                    "                    <a href=\"\" class=\"page-left\" onclick='pageNews("+pageNum+",10)'>《</a>\n" +
                    "                </div>";
                $("#news-pages").append(newsPages);
                //中间数字页码
                for (let i = 1; i <= data.pages; i++) {
                    let newsPages = "<div>\n" +
                        "                    <a href=\"#\" class=\"page-center\" onclick=\"pageNews("+i+",10)\" >"+i+"</a>\n" +
                        "                </div>";
                    $("#news-pages").append(newsPages);
                }
                //下一页
                let pageRight = data.pageNum>=data.pages?data.pages:data.pageNum+1;
                // console.log("下一页="+pageRight);
                let newsPage = "<div>\n" +
                    "                    <a href=\"#\" class=\"page-right\" onclick='pageNews("+pageRight+",10)'>》</a>\n" +
                    "                </div>";
                $("#news-pages").append(newsPage);
                let page = "<div>\n" +
                    "                    <span>当前页码：</span>\n" +
                    "                    <span>"+data.pageNum+"</span>\n" +
                    "                </div>";
                $("#news-pages").append(page);
                return false;
            }
            layer.msg('查询失败，请稍后重试！', {icon: 2, time: 1200});
            return false;
        },
        error: function () {
            layui.layer.msg('请求接口异常,请刷新重试！',{icon:0,time:1300});
            return false;
        }
    });

}